package com.ocom.payconfig.controller;

import com.ocom.common.constant.PayConfigUrl;
import com.ocom.common.entity.card.EntityXfRecordProcess;
import com.ocom.common.entity.payconfig.EntityOrderEvaluate;
import com.ocom.common.enums.ResultEnum;
import com.ocom.common.request.payconfig.EvaluateListRequest;
import com.ocom.common.resp.Result;
import com.ocom.common.utils.ResultUtil;
import com.ocom.common.utils.StringUtils;
import com.ocom.payconfig.service.OrderEvaluateService;
import com.ocom.payconfig.service.XfRecordProcessService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
public class OrderEvaluateController {
    @Autowired
    private OrderEvaluateService orderEvaluateService;

    @Autowired
    private XfRecordProcessService xfRecordProcessService;

    @PostMapping(PayConfigUrl.Url_evaluateList)
    public Result evaluateList(@RequestBody EvaluateListRequest request) {
        if (request == null || request.getComId() == null) {
            return ResultUtil.error(ResultEnum.Err_203);
        }
        Map<String, Object> dataMap = orderEvaluateService.evaluateList(request.getComId(), request);
        return ResultUtil.success(dataMap);
    }

    @PostMapping(PayConfigUrl.Url_evaluateAdd)
    public Result evaluateAdd(@RequestBody EntityOrderEvaluate request) {
        if (request == null || request.getComId() == null || StringUtils.isEmpty(request.getOrderNo())) {
            return ResultUtil.error(ResultEnum.Err_203);
        }
        //查询订单号是否存在
        EntityXfRecordProcess entityXfRecordProcess = xfRecordProcessService.selectTradeNo(request.getComId(), request.getOrderNo(), null);
        if (entityXfRecordProcess == null) {
            //原订单号不存在
            return ResultUtil.error(ResultEnum.Err_3036);
        }
        request.setMerchId(entityXfRecordProcess.getMerchId());
        request.setPointId(entityXfRecordProcess.getPointId());
        request.setPId(entityXfRecordProcess.getPId());
        Boolean isSuccess = orderEvaluateService.evaluateAdd(request.getComId(), request);
        if (!isSuccess) {
            return ResultUtil.error(ResultEnum.Err_2009);
        }
        return ResultUtil.success();
    }
}
